package com.shtel.paas.monitorcenter.monitoring.alarm.management.warn.notice.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.shtel.paas.monitorcenter.monitoring.alarm.management.warn.notice.entity.WarnNoticeEntity;
import com.shtel.paas.monitorcenter.monitoring.alarm.management.warn.notice.mapper.WarnNoticeMapper;
import com.shtel.paas.monitorcenter.monitoring.alarm.management.warn.notice.service.WarnNoticeService;
import com.shtel.paas.monitorcenter.monitoring.alarm.management.warn.notice.vo.WarningNoticeCountVo;
import com.shtel.paas.monitorcenter.monitoring.alarm.management.warn.notice.vo.XyDataVo;
import com.shtel.paas.monitorcenter.monitoring.alarm.management.warn.notice.vo.selectoptionValueVo;
import com.shtel.paas.monitorcenter.system.management.user.management.vo.MonitoringCenterUserVo;
import com.shtel.paas.monitorcenter.util.Page;

/**
 * 告警通知
 * 
 * @author zhaochuan.zhong
 *
 */

@Service
public class WarnNoticeServiceImpl implements WarnNoticeService {
	
	@Autowired
	private WarnNoticeMapper warnNoticeMapper;
	
	/**
	 * 用户信息列表数据获取
	 * @return 
	 */
	@Override
	public Page<MonitoringCenterUserVo> selectUserInfo(WarnNoticeEntity rmpEnt) {
		
		PageHelper.startPage(rmpEnt.getPage(), rmpEnt.getSize());
		List<MonitoringCenterUserVo> selectUserInfo = this.warnNoticeMapper.selectWarnNoticeInfo(rmpEnt);
		Page<MonitoringCenterUserVo> pageContent = Page.build(selectUserInfo);
		return pageContent;
	}

	/**
	 * 
	 */
	@Override
	public List<WarningNoticeCountVo> selectNowWarnNmAndHistoryWarnNm(
			WarnNoticeEntity rmpEnt) {
		
		List<WarningNoticeCountVo> selectAllAlarmCount = this.warnNoticeMapper.selectTableList(rmpEnt);
		return selectAllAlarmCount;
	}

	@Override
	public Map<String, Object> selectNowWarnNmForBar(WarnNoticeEntity rmpEnt) {
		
		Map<String,Object> results = new HashMap<>();
		List<XyDataVo> xy = this.warnNoticeMapper.selectXYCount(rmpEnt);
		List<String> earlyWarnList = new ArrayList<>();
		List<String> dangerList = new ArrayList<>();
		List<String> seriousList = new ArrayList<>();
		Set<String> title = new HashSet<>();
		Set<String> xDataList = new LinkedHashSet<>();
		//主机识别变量
		Boolean hostEarlyFlag = false;
		Boolean hostDangerFlag = false;
		Boolean hostDeriousFlag = false;
		
		//分布式数据库识别变量
		Boolean dataEarlyFlag = false;
		Boolean dataDangerFlag = false;
		Boolean dataDeriousFlag = false;
		
		//分布式缓存识别变量
		Boolean cacheEarlyFlag = false;
		Boolean cacheDangerFlag = false;
		Boolean cacheDeriousFlag = false;
		
		//消息中间件识别变量
		Boolean newsEarlyFlag = false;
		Boolean newsDangerFlag = false;
		Boolean newsDeriousFlag = false;
		
		//应用识别变量
		Boolean appliEarlyFlag = false;
		Boolean appliDangerFlag = false;
		Boolean appliDeriousFlag = false;
		
		for(XyDataVo emp:xy){
			Boolean empflag = false;
			if("主机监控".equals(emp.getType())){
				xDataList.add(emp.getType());
				if("预警".equals(emp.getLevels())){
					earlyWarnList.add(emp.getCounts());
					title.add(emp.getLevels());
					hostEarlyFlag = true;
					empflag = true;
				}
				if("危险".equals(emp.getLevels())){
					dangerList.add(emp.getCounts());
					title.add(emp.getLevels());
					hostDangerFlag = true;
					empflag = true;
				}
				if("严重".equals(emp.getLevels())){
					seriousList.add(emp.getCounts());
					title.add(emp.getLevels());
					hostDeriousFlag = true;
					empflag = true;
				}
			}
			if(hostEarlyFlag || hostDangerFlag || hostDeriousFlag){
				if(!hostEarlyFlag && !empflag){
					earlyWarnList.add("0");
					hostEarlyFlag = true;
				}
				if(!hostDangerFlag && !empflag){
					dangerList.add("0");
					hostDangerFlag = true;
				}
				if(!hostDeriousFlag && !empflag){
					seriousList.add("0");
					hostDeriousFlag = true;
				}
			}
			
			if("分布式数据库".equals(emp.getType())){
				xDataList.add(emp.getType());
				if("预警".equals(emp.getLevels())){
					earlyWarnList.add(emp.getCounts());
					title.add(emp.getLevels());
					dataEarlyFlag = true;
					empflag = true;
				}
				if("危险".equals(emp.getLevels())){
					dangerList.add(emp.getCounts());
					title.add(emp.getLevels());
					dataDangerFlag = true;
					empflag = true;
				}
				if("严重".equals(emp.getLevels())){
					seriousList.add(emp.getCounts());
					title.add(emp.getLevels());
					dataDeriousFlag = true;
					empflag = true;
				}
			}
			
			if(dataEarlyFlag || dataDangerFlag || dataDeriousFlag){
				if(!dataEarlyFlag && !empflag){
					earlyWarnList.add("0");
					dataEarlyFlag = true;
				}
				if(!dataDangerFlag && !empflag){
					dangerList.add("0");
					dataDangerFlag = true;
				}
				if(!dataDeriousFlag && !empflag){
					seriousList.add("0");
					dataDeriousFlag = true;
				}
			}

			if("消息中间件".equals(emp.getType())){
				xDataList.add(emp.getType());
				if("预警".equals(emp.getLevels())){
					earlyWarnList.add(emp.getCounts());
					title.add(emp.getLevels());
					newsEarlyFlag = true;
					empflag = true;
				}
				if("危险".equals(emp.getLevels())){
					dangerList.add(emp.getCounts());
					title.add(emp.getLevels());
					newsDangerFlag  = true;
					empflag = true;
				}
				if("严重".equals(emp.getLevels())){
					seriousList.add(emp.getCounts());
					title.add(emp.getLevels());
					newsDeriousFlag  = true;
					empflag = true;
				}
			}
			
			if(newsEarlyFlag || newsDangerFlag || newsDeriousFlag){
				if(!newsEarlyFlag  && !empflag){
					earlyWarnList.add("0");
					newsEarlyFlag  = true;
				}
				if(!newsDangerFlag  && !empflag){
					dangerList.add("0");
					newsDangerFlag  = true;
				}
				if(!newsDeriousFlag  && !empflag){
					seriousList.add("0");
					newsDeriousFlag  = true;
				}
			}
			
			
			if("分布式缓存".equals(emp.getType())){
				xDataList.add(emp.getType());
				if("预警".equals(emp.getLevels())){
					earlyWarnList.add(emp.getCounts());
					title.add(emp.getLevels());
					cacheEarlyFlag = true;
					empflag = true;
				}
				if("危险".equals(emp.getLevels())){
					dangerList.add(emp.getCounts());
					title.add(emp.getLevels());
					cacheDangerFlag  = true;
					empflag = true;
				}
				if("严重".equals(emp.getLevels())){
					seriousList.add(emp.getCounts());
					title.add(emp.getLevels());
					cacheDeriousFlag  = true;
					empflag = true;
				}
			}
			if(cacheEarlyFlag || cacheDangerFlag || cacheDeriousFlag){
				if(!cacheEarlyFlag  && !empflag){
					earlyWarnList.add("0");
					cacheEarlyFlag = true;
				}
				if(!cacheDangerFlag  && !empflag){
					dangerList.add("0");
					cacheDangerFlag  = true;
				}
				if(!cacheDeriousFlag  && !empflag){
					seriousList.add("0");
					cacheDeriousFlag  = true;
				}
			}


			if("应用".equals(emp.getType())){
				xDataList.add(emp.getType());
				if("预警".equals(emp.getLevels())){
					earlyWarnList.add(emp.getCounts());
					title.add(emp.getLevels());
					appliEarlyFlag = true;
					empflag = true;
				}
				if("危险".equals(emp.getLevels())){
					dangerList.add(emp.getCounts());
					title.add(emp.getLevels());
					appliDangerFlag  = true;
					empflag = true;
				}
				if("严重".equals(emp.getLevels())){
					seriousList.add(emp.getCounts());
					title.add(emp.getLevels());
					appliDeriousFlag  = true;
					empflag = true;
				}
			}
			if(appliEarlyFlag || appliDangerFlag || appliDeriousFlag){
				if(!appliEarlyFlag  && !empflag){
					earlyWarnList.add("0");
					appliEarlyFlag  = true;
				}
				if(!appliDangerFlag  && !empflag){
					dangerList.add("0");
					appliDangerFlag  = true;
				}
				if(!appliDeriousFlag  && !empflag){
					seriousList.add("0");
					appliDeriousFlag  = true;
				}
			}
		}
		results.put("X", xDataList);
		results.put("title", title);
		results.put("earlyWarnList", earlyWarnList);
		results.put("dangerList", dangerList);
		results.put("seriousList", seriousList);
		return results;
		
	}

	/**
	 * 搜索部分下拉框的值
	 */
	@Override
	public Map<String, Object> selectOptionValue() {
		
		Map<String, Object> results = new HashMap<String, Object>();
		List<selectoptionValueVo> temResult = new ArrayList<>();
		List<selectoptionValueVo> monitorStatusResult = new ArrayList<>();
		List<selectoptionValueVo> indexMonitorResult = new ArrayList<>();
		List<selectoptionValueVo> earlyWarnListResult = new ArrayList<>();
		List<selectoptionValueVo> selectedKpiIndex = new ArrayList<>();
		
		temResult = warnNoticeMapper.selectOptionValue();
		
		for(selectoptionValueVo tem:temResult){
			if("监控状态".equals(tem.getType())){
				monitorStatusResult.add(tem);
			}else if("监控指标".equals(tem.getType())){
				indexMonitorResult.add(tem);
			}else if("预警级别".equals(tem.getType())){
				earlyWarnListResult.add(tem);
			}else if("指标KPI".equals(tem.getType())){
				selectedKpiIndex.add(tem);
			}
		}
		
		results.put("monitorStatus", monitorStatusResult);
		results.put("monitorIndex", indexMonitorResult);
		results.put("earlyWarnLevel", earlyWarnListResult);
		return results;
	}
	
	
}
